﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UtilWpf
{
    /// <summary>
    /// UserControlDataGridListObj.xaml 的交互逻辑
    /// </summary>
    public partial class AucDataGridListObj : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public DataGrid Dg;
        /// <summary>
        /// 
        /// </summary>
        public AucDataGridListObj()
        {
            InitializeComponent();

            Dg = DataGridListObj;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="listColumnNode"></param>
        /// <param name="listObj"></param>
        /// <param name="autoGenerateColumns"></param>
        /// <param name="canUserAddRows"></param>
        public void BindingListObj<T>(List<UserControlColumnNode> listColumnNode, IEnumerable<T> listObj
            , bool autoGenerateColumns = false, bool canUserAddRows = false)
        {
            Dg.Columns.Clear();
            foreach (var item in listColumnNode)
            {
                Dg.Columns.Add(new DataGridTextColumn() { Header = item.Header, Binding = new Binding(item.Binding), Width = new DataGridLength(1, DataGridLengthUnitType.Star), });
            }
            var ListDatas = UtilWpfPub.GetUserControlDataGridListObj(listObj);
            Dg.ItemsSource = ListDatas;
            Dg.AutoGenerateColumns = autoGenerateColumns;
            Dg.CanUserAddRows = canUserAddRows;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="listColumnNode"></param>
        /// <param name="ListDatas"></param>
        /// <param name="autoGenerateColumns"></param>
        /// <param name="canUserAddRows"></param>
        public void BindingListData(List<UserControlColumnNode> listColumnNode, ObservableCollection<ExpandoObject> ListDatas
            , bool autoGenerateColumns = false, bool canUserAddRows = false)
        {
            Dg.Columns.Clear();
            foreach (var item in listColumnNode)
            {
                Dg.Columns.Add(new DataGridTextColumn() { Header = item.Header, Binding = new Binding(item.Binding), Width = new DataGridLength(1, DataGridLengthUnitType.Star), });
            }
            Dg.ItemsSource = ListDatas;
            Dg.AutoGenerateColumns = autoGenerateColumns;
            Dg.CanUserAddRows = canUserAddRows;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="listObj"></param>
        /// <param name="autoGenerateColumns"></param>
        /// <param name="canUserAddRows"></param>
        public void BindingListObj<T>(IEnumerable<T> listObj
            , bool autoGenerateColumns = false, bool canUserAddRows = false) where T : new()
        {
            Dg.Columns.Clear();
            var listColumnNode = UtilWpfPub.GetUserControlColumnNodeList<T>();
            foreach (var item in listColumnNode)
            {
                Dg.Columns.Add(new DataGridTextColumn() { Header = item.Header, Binding = new Binding(item.Binding), Width = new DataGridLength(1, DataGridLengthUnitType.Star), });
            }
            var ListDatas = UtilWpfPub.GetUserControlDataGridListObj(listObj);
            Dg.ItemsSource = ListDatas;
            Dg.AutoGenerateColumns = autoGenerateColumns;
            Dg.CanUserAddRows = canUserAddRows;
        }
    }
}
